package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
public class FileUploadController {
    @RequestMapping("/fileUpload")
    public String fileUpload(MultipartFile file , HttpServletRequest request ) throws IOException {
        String path = request.getServletContext().getRealPath("/");
        String uploadPath = "/static/upload/";
        String fileName = file.getOriginalFilename();
        String filePath = path + uploadPath + fileName;
        if (!file.isEmpty()){
            file.transferTo(new File(filePath));
            return "uploadSuccess";
        }
        return "uploadFailure";
    }
    @RequestMapping("/uploads")
    public String uploads(MultipartFile[] files , HttpServletRequest request ) throws IOException {
        String path = request.getServletContext().getRealPath("/");
        String uploadPath = "/static/upload/";
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            String filePath = path + uploadPath +fileName;
            if (!file.isEmpty()){
                file.transferTo(new File(filePath));
        }
    }
        return "uploadSuccess";
}
}
